﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _10_HPBar
{
    internal class AnimeSystem
    {
        struct AnimeData
        {
            public Pos pos;
            public int display; // enum
            public ConsoleColor color;
        }

        int x, y;
        int speed, frame, index;
        bool start;
        Dictionary<string, List<List<AnimeData>>> animeDatas =new();
        List<List<AnimeData>>? currentAnime;

        public AnimeSystem(int x,int y,int speed = 6)
        {
            this.x = x;
            this.y = y;
            this.speed = speed;
            index= 0;
            start = false;
            currentAnime = null;
            animeDatas.Add("Anime1", LoadData("Map/map",3));
        }

        public void Play(string animeName)
        {
            animeDatas.TryGetValue(animeName, out currentAnime);
            if (currentAnime == null) return;
            index = 0;
            start = true;
        }

        public void Render()
        {
            Console.WriteLine(DateTime.Now.Ticks);
            if (!start)return;
            frame++;
            if(frame > speed)
            {
                // 播放一帧动画
                if(currentAnime != null)
                {
                    Console.Clear();
                    if(index < currentAnime.Count)
                    {
                        foreach (var data in currentAnime[index])
                        {
                            if (data.display == 0)
                            {
                                Utility.MyPrint("■", data.pos.x + x, data.pos.y + y, data.color);
                            }
                        }
                    }
                    index++;
                    if (index > currentAnime.Count)
                    {
                        start = false;
                        //currentAnime = null;
                    }
                }
                frame = 0;
            }
        }

        List<List<AnimeData>> LoadData(string path,int frame)
        {
            List<List<AnimeData>> animeData = new List<List<AnimeData>>();
            for (int i = 0; i < frame; i++)
            {
                var str = DataSystem.Instance.LoadData($"../../../{path}{i + 1}.csv");
                var lines = str.Split('\n');
                List<AnimeData> pixelData = new List<AnimeData>();
                for (int j = 0; j < lines.Length;j++)
                {
                    var elements = lines[j].Split(',');
                    for (int k = 0;  k < elements.Length; k++)
                    {
                        AnimeData data = new AnimeData();
                        data.display = int.Parse(elements[k]);
                        data.pos.x = k;
                        data.pos.y = j;
                        data.color = ConsoleColor.Magenta;
                        pixelData.Add(data);
                    }
                }
                    animeData.Add(pixelData);
            }
            return animeData;
        }
    }
}
